require(bdynsys)
require(plm)
source("bdynsysf.R")
data.all <- read.csv("DataMethods.csv")
data.all[,names(data.all)[grep("5C",names(data.all))]] <- round(data.all[,names(data.all)[grep("5C",names(data.all))]] * 4)
pdata.all <- pdata.frame(data.all,c("country_me","year"))
require(devEMF)

# Models dynamical systems and outputs equations (uncomment below to run -- it takes at least an hour)
#output1 <- capture.output(bdynsys(pdata.all,3,4,pdata.all$v2x_freexp,pdata.all$v2xme_altinf,pdata.all$v2x_EDcomp_thick))
#write(output1,"bdynsys_freexp_altinf_EDcomp.txt")

# Models dynamical systems and outputs equations (uncomment below to run -- it takes at least an hour)
#output2 <- capture.output(bdynsys(pdata.all,2,6,pdata.all$v2x_freexp,pdata.all$v2x_EDcomp_thick))
#write(output2,"bdynsys2_freexp_EDcomp.txt")
# Plots dynamical system, using the result from above
f <- function(t,y) rbind(0.046*y[2]-0.08*y[1]*y[2]+0.035*y[2]^3,0.12*y[2]-0.45*y[2]^2+0.054*y[1]^3+0.3*y[2]^3)
phaseportdat.f(pdata.all,"v2x_freexp","v2x_EDcomp_thick",xlab="Freedom of expression",ylab="Electoral component index",c(0,1),c(0,1),f,c("Sweden","France","United_States","Russia","Egypt","Afghanistan"),0.1,T)

# Models dynamical systems and outputs equations (uncomment below to run -- it takes at least an hour)
#output3 <- capture.output(bdynsys(pdata.all,2,6,pdata.all$v2x_freexp,pdata.all$v2xme_altinf))
#write(output3,"bdynsys2_freexp_altinf.txt")
# Plots dynamical system, using the result from above
f <- function(t,y) rbind(-0.043*y[1]+0.034*y[2]+0.0062*y[1]/y[2],0.0077-0.039*y[1]*y[2]+0.032*y[1]^3)
phaseportdat.f(pdata.all,"v2x_freexp","v2xme_altinf",xlab="Freedom of expression",ylab="Alternative source information",c(0,1),c(0,1),f,c("Sweden","France","United_States","Russia","Egypt","Afghanistan"),0.1,T)
